﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class User_SysUserManager : PageBase
{
    private string action
    {
        get
        {
            if (ViewState["action"] == null) return "";
            else return ViewState["action"].ToString();
        }
        set { ViewState["action"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            action = Request.QueryString["action"].ToString();
            Databang(0);
            if (action == "add")
            {
                lblTitle.Text = "添加用户";                
            }
            else
            {
                lblTitle.Text = "编辑用户";

                InitData();
            }
            
        }
        
     }

    protected void InitData()
    {
        UserBLL BllUser = new UserBLL();
        UserName.ReadOnly = true;
        string ID = Request.QueryString["ID"].ToString();
        T_SYS_USER user = BllUser.GetUserByID(ID);
        UserName.Text = user.USER;
        RealName.Text = user.NAME;
        if (user.STATUS == 1) Status.SelectedIndex = 0;else  Status.SelectedIndex = 1;
        ListItem Item= TypeID.Items.FindByValue(user.ROLE_ID.ToString());
        TypeID.SelectedIndex = TypeID.Items.IndexOf(Item);

    }

    protected void Databang(int UserTypeID)
    {
        RoleBLL BllRole = new RoleBLL();
        TypeID.DataSource = BllRole.GetRoleList();
        TypeID.DataBind();


    }
    protected void Add_Submit_Click(object sender, EventArgs e)
    {
        UserBLL BllUser = new UserBLL();
        if (action == "add")
        {
            T_SYS_USER user = BllUser.GetUserByName(UserName.Text.Trim());
            if (user == null)
            {
                user = new T_SYS_USER();
                user.USER = UserName.Text.Trim();
                user.PASSWORD = StrHelper.EncryptPassword(PassWord.Text, StrHelper.PasswordType.MD5);
                user.NAME = RealName.Text;
                user.ROLE_ID = new Guid(TypeID.SelectedValue);
                user.STATUS = Convert.ToInt32(Status.SelectedValue);
                if (BllUser.Add(user) < 0)
                {
                    ShowMessage("添加失败");
                }
                else
                {
                    Response.Redirect("SysUserSearch.aspx");
                }
            }
            else
            {
                ShowMessage("该用户名已被使用，请输入其他用户名");
            }
        }
        else
        {
            string ID = Request.QueryString["ID"].ToString();
            T_SYS_USER user = BllUser.GetUserByID(ID);
            user.USER = UserName.Text.Trim();
            user.PASSWORD = StrHelper.EncryptPassword(PassWord.Text, StrHelper.PasswordType.MD5);
            user.NAME = RealName.Text;
            user.ROLE_ID = new Guid(TypeID.SelectedValue);
            user.STATUS = Convert.ToInt32(Status.SelectedValue);
            if (BllUser.Update(user) < 0)
            {
                ShowMessage("添加失败");
            }
            else
            {
                Response.Redirect("SysUserSearch.aspx");
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("SysUserSearch.aspx");
    }
}
